import {createSlice} from '@reduxjs/toolkit';
import {Toast} from "@douyinfe/semi-ui";
import {CreateAiConfig, DeleteAiConfig, ListAiConfig, SetCurrentAiConfig, UpdateAiConfig} from "@/service/ai.ts";
import {AiState} from "@/schemas/ai.ts"; // 确保路径正确

// 定义 user state 的初始值
const initialState: AiState = {
    list: [],
    loading: false
};

const aiSlice = createSlice({
    name: 'ai',
    initialState,
    reducers: {},
    extraReducers: (builder) => {

        [
            ListAiConfig, CreateAiConfig, UpdateAiConfig,
            DeleteAiConfig, SetCurrentAiConfig
        ].forEach(thunk => {
            builder.addCase(thunk.pending, (state) => {
                state.loading = true
            })
            builder.addCase(thunk.rejected, (state, action) => {
                state.loading = false
                Toast.error(action.error.message || "数据操作出现未知错误")
            })
            builder.addCase(thunk.fulfilled, (state, action) => {
                state.loading = false
                state.list = action.payload
            })
        })
    }
});

// 导出 actions
export const AI = aiSlice.actions;

// 导出 reducer
export default aiSlice.reducer;